package com.jiangjiang.eshop.customer.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangjiang.eshop.customer.dao.ReturnGoodsWorksheetMapper;
import com.jiangjiang.eshop.customer.domain.ReturnGoodsWorksheetEntity;
import com.jiangjiang.eshop.customer.dto.ReturnGoodsQueryPageDTO;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

/**
 * @Author Jianghaowen
 * @Date 2021/9/9
 * @Description: TODO
 * @Version V1.0
 **/
@Service
public class ReturnGoodsWorksheetService extends ServiceImpl<ReturnGoodsWorksheetMapper, ReturnGoodsWorksheetEntity> {

    /**
     * 退货工单分页查询
     *
     * @param dto 查询条件
     * @return 分页结果
     */
    public Page<ReturnGoodsWorksheetEntity> queryForPage(ReturnGoodsQueryPageDTO dto) {
        Page<ReturnGoodsWorksheetEntity> page = new Page<>(dto.getPageNo(), dto.getPageSize());
        LambdaQueryWrapper<ReturnGoodsWorksheetEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(dto.getId() != null, ReturnGoodsWorksheetEntity::getId, dto.getId())
                .eq(dto.getOrderInfoId() != null, ReturnGoodsWorksheetEntity::getOrderInfoId, dto.getOrderInfoId())
                .eq(Strings.isNotEmpty(dto.getOrderNo()), ReturnGoodsWorksheetEntity::getOrderNo, dto.getOrderNo())
                .eq(dto.getStatus() != null, ReturnGoodsWorksheetEntity::getStatus, dto.getStatus())
                .eq(dto.getReturnGoodsReason() != null, ReturnGoodsWorksheetEntity::getReturnGoodsReason, dto.getReturnGoodsReason())
                .eq(Strings.isNotEmpty(dto.getReturnGoodsRemark()), ReturnGoodsWorksheetEntity::getReturnGoodsRemark, dto.getReturnGoodsRemark())
                .eq(Strings.isNotEmpty(dto.getReturnGoodsLogisticsCode()), ReturnGoodsWorksheetEntity::getReturnGoodsLogisticsCode, dto.getReturnGoodsLogisticsCode());
        return this.getBaseMapper().selectPage(page, wrapper);
    }


    /**
     * 根据订单编号查询退货工单
     * @param orderInfoId 订单编号数据
     * @return 查询结果
     */
    public ReturnGoodsWorksheetEntity queryByOrderInfoId(Long orderInfoId){
        LambdaQueryWrapper<ReturnGoodsWorksheetEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ReturnGoodsWorksheetEntity::getOrderInfoId, orderInfoId);
        return this.getOne(wrapper);
    }

    /**
     * 退货工单状态更新
     *
     * @param id     退货工单id
     * @param status 退货工单状态
     * @return 处理结果
     */
    public Boolean updateStatus(Long id, Integer status) {
        LambdaUpdateWrapper<ReturnGoodsWorksheetEntity> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(ReturnGoodsWorksheetEntity::getId, id)
                .set(ReturnGoodsWorksheetEntity::getStatus, status);
        return this.update(wrapper);
    }

}
